package com.crm.controller;


import com.crm.pojo.common.PageResult;
import com.crm.pojo.common.Result;
import com.crm.pojo.dto.RoleDto;
import com.crm.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/role")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;
    //角色列表查询
    @GetMapping("/list")
    public PageResult list(@RequestParam(required = false) Long roleId,
                           @RequestParam(required = false) String roleName,
                           @RequestParam(required = false) String roleKey,
                           @RequestParam(required = false) String status,
                           @RequestParam(value = "params[beginTime]", required = false) String beginCreateTime,
                           @RequestParam(value = "params[endTime]", required = false) String endCreateTime){
        RoleDto roleDto = new RoleDto();
        roleDto.setRoleId(roleId);
        roleDto.setRoleName(roleName);
        roleDto.setRoleKey(roleKey);
        roleDto.setStatus(status);
        roleDto.setBeginCreateTime(beginCreateTime);
        roleDto.setEndCreateTime(endCreateTime);
        return roleService.list(roleDto);
    }

    //角色添加
    @PostMapping
    public Result add(@RequestBody RoleDto roleDto){
        try {
            roleService.add(roleDto);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.success();
    }

    //角色权限状态启用/禁用
    @PutMapping("/changeStatus")
    public Result changeStatus(@RequestBody RoleDto roleDto){
        try {
            roleService.changeStatus(roleDto);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
        return Result.success();
    }
}
